﻿using Microsoft.Extensions.DependencyInjection;

namespace Luka;

/// <summary>
/// 异常捕获
/// </summary>
[NotScanned]
public static class FriendlyExceptionSetup
{
    /// <summary>
    /// 注册异常捕获
    /// </summary>
    /// <returns></returns>
    public static IServiceCollection AddFriendlyException(this IServiceCollection services)
    {
        // 查找实现类
        var handler = InternalApp.TypeList.FirstOrDefault(f => typeof(IExceptionHandler).IsAssignableFrom(f) && !f.IsInterface);
        if (handler != null)
        {
            services.AddSingleton(typeof(IExceptionHandler), handler);
        }
        // 全局过滤器
        services.AddFilter<FriendlyExceptionFilter>();
        return services;
    }
}